package com.ve.view.editor.span;

import android.graphics.Paint;
import android.util.Log;

import java.util.List;

public class SpanSeeker {
    private static final String TAG = SpanSeeker.class.getSimpleName();
    private Paint paint;
    private SpanNode currentSpan, nextSpan;
    private int index = 0;
    private int spanPosition = 0;
    private List<SpanNode> spanNodes;


    public SpanSeeker(TextSpanData data, Paint paint) {
        this.paint = paint;
        this.spanNodes = data.getSpanNodes();
    }

    public void begin(int currentPosition) {

        index = 0;
        spanPosition = 0;
        nextSpan = spanNodes.get(index++);
        do {
            currentSpan = nextSpan;
            spanPosition += currentSpan.length;
            if (index < spanNodes.size()) {
                nextSpan = spanNodes.get(index++);
            } else {
                nextSpan = null;
            }
        } while (nextSpan != null && spanPosition <= currentPosition);

        currentSpan.type.onSpan(paint);
    }


    public void listenSpan(int currentPosition) {
        if (nextSpan != null && currentPosition >= spanPosition) {
            currentSpan = nextSpan;
            spanPosition += currentSpan.length;
            currentSpan.type.onSpan(paint);


            if (index < spanNodes.size()) {
                nextSpan = spanNodes.get(index++);
            } else {
                nextSpan = null;
            }

        }
    }

    public SpanNode getCurrentSpan() {
        return currentSpan;
    }
}